#include <iostream>
#include "Log.hpp"
#include "UdpServer.hpp"
#include <memory>
#include <vector>
#include <cstdio>
void Usage(std::string proc)
{
    std::cout << "Usage:\n\t" << proc << "local_port\n";
}

std::vector<std::string> black_words =
    {
        "rm",
        "cp",
        "exit",
        "vim"};

bool SafeCheck(std::string command)
{
    for (auto &k : black_words)
    {
        std::size_t pos = command.find(k);
        if (pos != std::string::npos)
            return false;
    }
    return true;
}
std::string ExecuuteCommand(std::string command)
{
    if (!SafeCheck(command))
        return "bad man";
    std::cout << "get message:" << command << std::endl;
    FILE *fp=popen(command.c_str(),"r");
  
    if (fp == nullptr) 
    {
        return "execute error,command unkown";
    }
    else{
        std::cout<<"fp不为空";
    }

    std::string response;
    char buffer[1024];
    while (true)
    {
        char *s = fgets(buffer, sizeof(buffer), fp);
        if (!s)
            break;
        else
            response += buffer;
    }
    pclose(fp);
    return response.empty() ? "success" : response;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return Usage_Err;
    }

    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> usvr = std::make_unique<UdpServer>(ExecuuteCommand, port);
    usvr->Init();
    usvr->Start();

    return 0;
}